package com.eboss2.sbs.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.eboss2.sbs.R;
import com.eboss2.sbs.activity.base.BaseActivity;
import com.eboss2.sbs.adapter.CategoryListAdapter;
import com.eboss2.sbs.config.Config;
import com.eboss2.sbs.http.HttpUtil;
import com.eboss2.sbs.model.CategoryListInfo;
import com.eboss2.sbs.myapplication.MyApplication;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.loopj.android.http.RequestParams;
import com.mytools.tool.activityManager.MyActivityManager;
import com.mytools.tool.utils.StringUtil;
import com.mytools.tool.utils.StringUtils;
import com.mytools.tool.views.MyListView;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

public class MyEmsModleyListActivity extends BaseActivity implements View.OnClickListener{
	
	private CategoryListInfo vo;
	private List<CategoryListInfo> categoryListInfos = new ArrayList<>();
	private MyListView listview;
	private CategoryListAdapter categoryListAdapter;
	private TextView tv_category_text;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		mContext = this;
		setContentView(R.layout.activity_category_choice);
		MyActivityManager.getInstance().addActivity(this);
		initView();
		doSubmit();
	}

	private void testData() {
		for(int i = 0; i<5; i++){
			CategoryListInfo categoryListInfo = new CategoryListInfo();
			categoryListInfo.setId(i);
			categoryListInfo.setName(i+"0");
			categoryListInfos.add(categoryListInfo);
		}
	}

	private void initView(){
		RelativeLayout relativeLayout = relativeLayout(R.id.lv_heard);
		relativeLayout.setBackgroundColor(getResources().getColor(R.color.statusBar));
		showView(R.id.titleback_btn_back, true);
		setTvText(R.id.tv_title, "运费模板选择");
		findViewById(R.id.titleback_btn_back).setOnClickListener(this);
		tv_category_text = textView(R.id.tv_category_text);
		tv_category_text.setText("模板");


		listview = findViewById(R.id.listview);
		categoryListAdapter = new CategoryListAdapter(mContext, categoryListInfos);
		listview.setAdapter(categoryListAdapter);
		listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
				vo = categoryListInfos.get(position);
				for (int i = 0; i < categoryListInfos.size(); i++){
					categoryListInfos.get(i).setShow(false);
				}
				vo.setShow(true);
				categoryListAdapter.notifyDataSetChanged();

				setResult(7, (new Intent()).putExtra("emsMethodId", vo.getId()).putExtra("emsName", vo.getName()));
				finish();
			}
		});
	}



	/**
	 * @Description (TODO作用)
	 */
	private void doSubmit() {


		String url = Config.SBS_LIST_TEMPLATE;

		RequestParams params = new RequestParams();
		params.put("groupId", MyApplication.getmInstance().getLoginMEntity().getGroupId());

		showProgress("提交中...");

		HttpUtil.get(url, params, new HttpUtil.RequestListener() {

			@Override
			public void success(String response) {
				disShowProgress();


				if (!StringUtils.isBlank(response)){
					try {
						List<CategoryListInfo> datas = new Gson().fromJson(response, new TypeToken<List<CategoryListInfo>>(){}.getType());
						if (datas != null && datas.size() > 0){
							categoryListInfos.clear();
							categoryListInfos.addAll(datas);
							categoryListAdapter.notifyDataSetChanged();
						}
					} catch (Exception e) {
						e.getMessage();
					}
				}
			}

			@Override
			public void failed(Throwable error) {
				disShowProgress();
				showE404();
			}
		});

	}


	
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
			case R.id.titleback_btn_back:
				finish();
				break;
			default: break;
		}
	}
	

	
}
